// discovery/baseDetail/baseDetail.js
var app=getApp();
var util=require("../../utils/util");
var base=require("../../api/base");
var WxParse = require('../../wxParse/wxParse.js');

Page({
  /*页面的初始数据*/
  data: {
    info: '',
    id: '',
    navColor: 'rgba(0,0,0,0)',
    title: '',
    back_type: 1,
    com_list: [],
    focus: false,
    reply: false,
    com_type: 1,
    com_index: null,
    com_kcs_index: null,
    com_kcs_type: 0,
    place_text: '评论是有影响力的',
    text: '',
    currentPage: 1,
    pageSize: 10,
    hasMoreDate: true,
    token: '',
    userinfo: '',
    pop_show: true,
    commentCount: 0
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that=this;
    if(options.type){
      that.setData({back_type:options.type})
    }
    that.setData({id:options.id})
    //
    that.get_base_detail(options.id)
  },
  wxParseImgTap:function(e){
    var id=e.currentTarget.dataset.alt;
    if(id){
      wx.navigateTo({
        url: '../../pages/courseDetail/courseDetail?id='+id,
      })
    }
  },
  // 页面滚动
  onPageScroll:function(e){
    var that = this,info=that.data.info;
    var scroll = e.scrollTop;
    if(scroll >= 160){
      that.setData({navColor: '#ffffff',title:info.title})
    }else{
      that.setData({navColor: 'rgba(0,0,0,0)',title:''})
    }
  },
  // 收藏知识库
  call_base:function(){
    var that=this,info=that.data.info,token=that.data.token;
    if(token){
      if(info.collectionId){
        //取消
        that.cancel_call_base(info.collectionId,token)
      }else{
        // 收藏
        that.add_call_base(token);
      }
    }else{
      wx.navigateTo({
        url: '../../pages/login/login',
      })
    }
  },
  // 赞评论
  zan_comment:function(e){
    var that = this,com_list=that.data.com_list,type=2,token=that.data.token;
    var index=e.currentTarget.dataset.index;
    if(token){
      if(com_list[index].praiseId){
        //
        that.cancel_praise(type,com_list[index].praiseId,index,token)
      }else{
        //
        that.add_praise(type,index,token)
      }
    }else{
      wx.navigateTo({
        url: '../../pages/login/login',
      })
    }
  },
  // 赞知识
  zan_base:function(){
    var that = this,info=that.data.info,type=1,token=that.data.token;
    if(token){
      if(info.praiseId){
        //
        that.cancel_praise(type,info.praiseId,0,token)
      }else{
        //
        that.add_praise(type,0,token)
      }
    }else{
      wx.navigateTo({
        url: '../../pages/login/login',
      })
    }
  },
  // 回复(或打开删除弹窗)
  user_input_focus:function(e){
    var that=this,com_list=that.data.com_list,userinfo=that.data.userinfo;
    var index=e.currentTarget.dataset.index,idx=e.currentTarget.dataset.idx,type=e.currentTarget.dataset.type;
    if(type==2){
      if(com_list[index].knowledgeComments[idx].createBy==userinfo.id){
        that.setData({pop_show:false,com_index:index,com_kcs_index:idx,com_kcs_type:type})
      }else{
        var text='回复'+com_list[index].knowledgeComments[idx].createAuthor+':'
        that.setData({focus:true,com_type:2,com_index:index,com_kcs_index:idx,com_kcs_type:type,reply:true,place_text:text})
      }
    }else{
      var text='回复'+com_list[index].createAuthor+':'
      that.setData({focus:true,com_type:2,com_index:index,com_kcs_index:idx,com_kcs_type:type,reply:true,place_text:text})
    }
  },
  del_tap:function(e){
    var that=this;
    var index=e.currentTarget.dataset.index,type=e.currentTarget.dataset.type,com_list=that.data.com_list,userinfo=that.data.userinfo;
    if(userinfo){
      if(com_list[index].createBy==userinfo.id){
        that.setData({com_index:index,com_kcs_type:type,pop_show:false})
      }
    }else{
      wx.navigateTo({
        url: '../../pages/login/login',
      })
    }
  },
  // 删除弹窗
  close_pop:function(){
    this.setData({pop_show:!this.data.pop_show})
  },
  // 评论
  input_base:function(){
    this.setData({com_type: 1})
  },
  input_focus:function(e){
    this.setData({focus: true,place_text: '健康交流，共建素质学习圈'})
  },
  input_blur:function(){
    this.setData({focus: false,reply:false,place_text: '评论是有影响力的'})
  },
  // 发送评论
  confirm_send:function(e){
    var that = this,info=that.data.info,com_type=that.data.com_type,com_index=that.data.com_index,com_list=that.data.com_list,com_kcs_type=that.data.com_kcs_type,com_kcs_index=that.data.com_kcs_index,token=that.data.token,testSpace = /^[ ]*$/g;
    var value=e.detail.value;
    // console.log(com_type)
    if(!value||testSpace.test(value)){
      util.page_show_toast('请输入评论内容',2000)
      that.setData({text:''})
      return false;
    }
    if(token){
      var params={
        "knowledgeId": info.id,
        "createTime": Date.parse(new Date()),
        "commentContent": value,
        "status": 1,
        "commentLevel": com_type
      }
      if(com_type==2){
        if(com_kcs_type==1){
          params.commentId=com_list[com_index].id
          params.reviewedId=''
          params.reviewedAuthor=''
        }else{
          params.commentId=com_list[com_index].id
          params.reviewedId=com_list[com_index].knowledgeComments[com_kcs_index].createBy
          params.reviewedAuthor=com_list[com_index].knowledgeComments[com_kcs_index].createAuthor
        }
      }
      that.setData({text: ''});
      that.add_comment(params,com_index,token)
    }else{
      wx.navigateTo({
        url: '../../pages/login/login',
      })
    }
  },
  // 展开评论
  open_more:function(e){
    var that=this,com_list=that.data.com_list;
    var index=e.currentTarget.dataset.index;
    com_list[index].more_com=true;
    com_list[index].com_child_index=(com_list[index].knowledgeComments).length
    that.setData({com_list:com_list})
  },
  close_more:function(e){
    var that=this,com_list=that.data.com_list;
    var index=e.currentTarget.dataset.index;
    com_list[index].more_com=false;
    com_list[index].com_child_index=2
    that.setData({com_list:com_list})
  },
  // 删除评论
  del_comment:function(e){
    var that=this,index=that.data.com_index,idx=that.data.com_kcs_index,com_kcs_type=that.data.com_kcs_type,com_list=that.data.com_list;
    var params={
      "id": com_kcs_type==1?com_list[index].id:com_list[index].knowledgeComments[idx].id,
      "commentLevel": com_kcs_type,
      "status": 0
    }
    that.user_del_comment(params,index,idx)
  },
  // 用户分享
  onShareAppMessage:function(){
    var that=this,id=that.data.id,info=that.data.info;
    that.add_share(id)
    return{
      title: info.title,
      path: '/discovery/baseDetail/baseDetail?id='+id+'&type=2',
      imageUrl: info.topicPic
    }
  },
  onShareTimeline:function(){
    var that=this,info=that.data.info;
    return{
      title: info.title,
      query: 'id='+info.id,
      imageUrl: info.topicPic
    }
  },
  onShow:function(){
    var that=this,token=that.data.token;
    var token_again=wx.getStorageSync('token'),userinfo=wx.getStorageSync('userinfo')
    if(!token&&token_again){
      that.setData({token: token_again,userinfo:userinfo})
    }
  },
  // 触底
  onReachBottom:function(){
    var that=this,hasMoreData=that.data.hasMoreData,id=that.data.id;
    if(hasMoreData){
      that.get_comment_list(id)
    }else{
      util.page_show_toast('没有更多评论了',1500)
    }
  },
  // 获取详情
  get_base_detail:function(id){
    wx.showLoading({title: '加载中'})
    var that=this;
    base.get_base_detail({id:id}).then(res=>{
      if(res.data.code==200){
        var info=res.data.data;
        info.viewCount=info.viewCount?(info.viewCount>=10000?parseFloat(info.viewCount/10000)+"万":info.viewCount):0
        info.commentCount=info.commentCount?info.commentCount:0
        info.praiseCount=info.praiseCount?info.praiseCount:0
        WxParse.wxParse('article', 'html', info.content, that, 5);
        that.setData({info: info})
        //
        that.get_comment_list()
        that.add_browse(info.id)
      }
    })
  },
  // 评论列表
  get_comment_list: function(){
    wx.showLoading({title: '加载中'})
    var that=this,id=that.data.id,currentPage=that.data.currentPage,pageSize=that.data.pageSize;
    var params={
      "currentPage": currentPage,
      "pageSize": pageSize,
      "commentLevel": 1,
      "knowledgeId": id,
    };
    base.get_comment_list(params).then(res=>{
      if(res.data.code==200){
        var list=res.data.data.records;
        for(var i=0;i<list.length;i++){
          list[i].createTime=util.formatTime_com_time(list[i].createTime)
          list[i].show_num=false
          list[i].more_com=false
          list[i].com_child_index=2
        }
        var com_list = that.data.com_list;
        if(com_list.length==0){
          that.setData({commentCount:res.data.data.total})
        }
        com_list = util.extend(com_list, list)
        if (list.length < pageSize) {
          that.setData({
            com_list: com_list,
            hasMoreData: false
          })
        } else {
          that.setData({
            com_list: com_list,
            hasMoreData: true,
            currentPage: currentPage + 1
          })
        }
      }
      
    })
  },
  // 收藏
  add_call_base: function(token){
    var that=this,info=that.data.info,userinfo=that.data.userinfo;
    var params={
      "createTime": new Date().getTime(),
      "knowledgeId": info.id,
      "createBy": userinfo.id
    }
    base.add_call_base(params,token).then(res=>{
      if(res.data.code==200){
        var id=res.data.data;
        info.collectionId=id;
        that.setData({info:info});
        util.page_show_toast('收藏成功',1500)
      }
    })
  },
  // 取消收藏
  cancel_call_base: function(id,token){
    var that=this,info=that.data.info;
    var params={"id":id};
    base.cancel_call_base(params,token).then(res=>{
      if(res.data.code==200){
        info.collectionId=null;
        that.setData({info:info});
        util.page_show_toast('取消收藏成功',1500)
      }
    })
  },
  // 新增评论
  add_comment:function(params,index,token){
    var that=this,com_list=that.data.com_list,info=that.data.info,userinfo=that.data.userinfo,com_kcs_type=that.data.com_kcs_type,commentCount=that.data.commentCount;
    base.add_comment(params,token).then(res=>{
      if(res.data.code==200){
        var data = {}
        data['commentContent']=params.commentContent
        data['createTime']=util.formatTime_com_time(params.createTime)
        data['knowledgeId']=params.knowledgeId
        data['praiseId']=null
        data['commentLevel']=params.commentLevel
        data['createBy']=userinfo.id
        data['createAuthor']=userinfo.nickName
        data['img']=userinfo.avatar
        if(params.commentLevel==1){
          data['id'] = res.data.data
          data['praiseCount'] = 0
          data['knowledgeComments'] = []
          com_list.unshift(data)
          commentCount +=1
        }else{
          data['id'] = res.data.data
          data['commentId'] = params.commentId
          data['reviewedId'] = params.reviewedId
          data['reviewedAuthor'] = params.reviewedAuthor
          com_list[index].knowledgeComments.push(data)
          com_list[index].more_com=true
          com_list[index].com_child_index=com_list[index].knowledgeComments.length+1
        }
        that.setData({com_list:com_list,info:info,commentCount:commentCount})
        util.page_show_toast('评论成功',1500)
      }else{
        util.page_show_toast(res.data.message,2000)
      }
    })
  },
  // 点赞
  add_praise:function(type,index,token){
    var that=this,info=that.data.info,com_list=that.data.com_list,userinfo=that.data.userinfo;
    var params={
      "createTime": Date.parse(new Date()),
      "knowledgeId": info.id,
      "type": type,
      "commentId": type==1?0:com_list[index].id,
      "createBy": userinfo.id
    }
    base.add_praise(params,token).then(res=>{
      if(res.data.code==200){
        var id=res.data.data;
        if(type==2){
          com_list[index].praiseId=id;
          com_list[index].praiseCount += 1;
          com_list[index].show_num=true
          that.setData({com_list:com_list})
          setTimeout(function(){
            com_list[index].show_num=false
            that.setData({com_list:com_list})
          },1500)
        }else{
          info.praiseId=id;
          info.praiseCount += 1;
          that.setData({info: info})
        }
        util.page_show_toast('点赞成功',1500)
      }
    })
  },
  // 取消点赞
  cancel_praise:function(type,id,index,token){
    var that=this,info=that.data.info,com_list=that.data.com_list;
    var params={"id":id};
    base.cancel_praise(params,token).then(res=>{
      if(res.data.code==200){
        if(type==2){
          com_list[index].praiseId=null;
          com_list[index].praiseCount -= 1;
          that.setData({com_list: com_list})
        }else{
          info.praiseId=null;
          info.praiseCount -= 1;
          that.setData({info: info})
        }
      }
    })
  },
  // 浏览
  add_browse:function(id){
    wx.showLoading({title: '加载中'})
    var that=this,userinfo=that.data.userinfo;
    var params={
      "createTime": Date.parse(new Date()),
      "knowledgeId": id,
      "createBy":  userinfo?userinfo.id:0
    }
    base.add_browse(params).then(res=>{
      // console.log(res)
    })
  },
  //分享
  add_share:function(id){
    var that=this,userinfo=that.data.userinfo;
    var params={
      "createTime": Date.parse(new Date()),
      "knowledgeId": id,
      "createBy": userinfo?userinfo.id:0
    }
    base.add_share(params).then(res=>{
      // console.log(res)
    })
  },
  // 删除评论
  user_del_comment:function(params,index,idx){
    var that=this,com_list=that.data.com_list,info=that.data.info,commentCount=that.data.commentCount;
    base.del_comment(params).then(res=>{
      if(res.data.code==200){
        that.close_pop()
        util.page_show_toast('删除成功',1500)
        if(params.commentLevel==1){
          com_list.splice(index,1)
          commentCount=commentCount-1
        }else{
          com_list[index].knowledgeComments.splice(idx,1)
        }
        that.setData({com_list:com_list,info:info,commentCount:commentCount})
      }
    })
  },
})